#if 0
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
// #include "MQTTAsync.h"
#include "MQTTClient.h"
#include "mqtt_client.h"
#include "unit.h"
#include "esn.h"
#include "cJSON.h"

#define CLIENTID        "DevinfoMQTTClient123"
#define BROKER_ADDRESS  "192.168.100.1:1883"
#define TOPIC_SUB       "SecurityProxy/get/response/devinfo/SerialNumInfo"
#define TOPIC_PUB       "devinfo/get/request/SecurityProxy/SerialNumInfo"
#define QOS              1
#define TIMEOUT          10000L

MQTTClient client;
// static int mqtt_state = MQTT_CLIENT_CREAT;  /* mqtt状态机 */
volatile MQTTClient_deliveryToken deliveredtoken;
response_msg response_msg_t;

/**
  * @brief  发布消息
  * @param  topic 消息主题
  * @param  payload 消息内容
  * @param  payload_len 消息内容字节数
  * @retval null
  */
static int mqtt_publish_message(char *topic, char *payload, int payload_len)
{
    MQTTClient_message pubmsg = MQTTClient_message_initializer;
    MQTTClient_deliveryToken token;
    int rc;

    if ((NULL == topic) || (NULL == payload)) return -1;

    pubmsg.payload = payload;
    pubmsg.payloadlen = payload_len;
    pubmsg.qos = QOS;
    pubmsg.retained = 0;

    if ((rc = MQTTClient_publishMessage(client, topic, &pubmsg, &token)) != MQTTCLIENT_SUCCESS) {
        return -2;
    }

    return 0;
}

static void delivered(void *context, MQTTClient_deliveryToken dt)
{
    deliveredtoken = dt;
}

/**
  * @brief  mqtt客户端与broker连接断开的时候会调用这个函数
  * @param  null
  * @retval null
  */
static void connlost(void *context, char *cause)
{
    printf("func(%s) MQTT Connection lost, cause: %s \n", __FUNCTION__, cause);
}

/**
  * @brief  mqtt订阅主题
  * @param  p_client mqtt客户端
  * @param  topic 要订阅的主题
  * @param  qos
  * @retval null
  */
static int subscribe_topic(MQTTClient *p_client, char *topic, int qos)
{
    int rc;

    if ((rc = MQTTClient_subscribe(*p_client, topic, qos)) != MQTTCLIENT_SUCCESS) {
        rc = EXIT_FAILURE;
    } 

    return rc;
}

/**
  * @brief  获取esn号应答
  * @param  null
  * @retval null
  */ 
static int topic_response_esn(void)
{
    cJSON *jn = NULL;
    int i;
    int size = 0;
    char temp[40];

    /* 解析收到的报文 */
    if (!strcmp(response_msg_t.topic, TOPIC_SUB))
    {
        jn = cJSON_Parse(response_msg_t.msg);
        if(jn == NULL)
	    {
		    cJSON_Delete(jn);
		    return -1;
	    }

        cJSON *token_item = cJSON_GetObjectItem(jn, STR_TOKEN);
        if (token_item == NULL)
        {
            cJSON_Delete(jn);
            return -1;
        }
        
        cJSON *time_item = cJSON_GetObjectItem(jn, STR_TIMESTAMP);
        if (time_item == NULL)
        {
            cJSON_Delete(jn);
            return -1;
        }
        
        cJSON *body = cJSON_GetObjectItem(jn, STR_BODY);	
	    if(body == NULL)
	    {
		    cJSON_Delete(jn);
		    return -1;
	    }
        
        size = cJSON_GetArraySize(body);
        for (i = 0; i < size; i++) 
        {
            cJSON *array_item = cJSON_GetArrayItem(body, i);

            cJSON *simslot_item = cJSON_GetObjectItem(array_item, STR_SERIALNUM);
            if ((NULL == simslot_item) || (NULL == simslot_item->valuestring)) 
            {
                cJSON_Delete(jn);
		        return -1;
            }
            
            cJSON *chipType = cJSON_GetObjectItem(array_item, STR_CHIPTYPE);	
	        if((chipType == NULL) || (chipType->valuestring == NULL))
	        {
		        cJSON_Delete(jn);
		        return -1;
	        }

            memset(temp, 0x00, sizeof(temp));
            memset(encrypt_t.sn, 0x00, sizeof(encrypt_t.sn));
            memset(encrypt_t.chiptype, 0x00, sizeof(encrypt_t.chiptype));
            memcpy(temp, simslot_item->valuestring, strlen(simslot_item->valuestring));
            /* 接收32位sn码仅需取前16位 */
            for (int j = 0; j <= 15; j++)
            {
                encrypt_t.sn[j] = temp[j];
            }
            
            sprintf(encrypt_t.chiptype, "%s", chipType->valuestring);
            
        }
        response_msg_t.flag = 0;
    }
    cJSON_Delete(jn);
    return 0;

}

/**
  * @brief  获取esn号请求
  * @param  null
  * @retval null
  */ 
static int topic_request_esn(void)
{  
    char resource_str[200];
	char time[100];
	
    memset(time, 0x00, sizeof(time));
	get_current_time_global(time);

	char resoure_str_format[100] = "{\"token\":\"123\",\"timestamp\":\"%s\",\"body\":[]}";

    memset(resource_str, 0x00, sizeof(resource_str));
	sprintf(resource_str, resoure_str_format, time);

    mqtt_publish_message(TOPIC_PUB, resource_str, strlen(resource_str));
  
    return 1;

}

/**
  * @brief  mqtt有消息到达
  * @param  null
  * @retval null
  */
static int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)
{
    memset(response_msg_t.topic, 0x00, sizeof(response_msg_t.topic));
    memset(response_msg_t.msg, 0x00, sizeof(response_msg_t.msg));
    memcpy(response_msg_t.topic, topicName, strlen(topicName));
    memcpy(response_msg_t.msg, message->payload, message->payloadlen);
    MQTTClient_freeMessage(&message);
    MQTTClient_free(topicName);
    response_msg_t.flag = 1;

#if 0 
    printf("topic:%s\n", response_msg_t.topic);
    printf("flag：%d\n", response_msg_t.flag);
    printf("%s\n", response_msg_t.msg);
#endif

    return 1;
}

int mqtt_init(void)
{
    MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;

    int rc;
    int i = 0;

    response_msg_t.flag = 0;

    if ((rc = MQTTClient_create(&client, BROKER_ADDRESS, CLIENTID,
        MQTTCLIENT_PERSISTENCE_NONE, NULL)) != MQTTCLIENT_SUCCESS)
    {
        return -1;
    }

    if ((rc = MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered)) != MQTTCLIENT_SUCCESS)
    {
        return -1;
    }

    conn_opts.keepAliveInterval = 20;
    conn_opts.cleansession = 1;

    if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
    {
        return -1;
    }

    subscribe_topic(&client, TOPIC_SUB, QOS);

    if (MQTTClient_isConnected(client))
	{
        topic_request_esn();

        for (int i = 0; i < 100; i++)
        {
            delay_ms(10);

            if (response_msg_t.flag)
            {
                topic_response_esn();
                return 0;
            }

            if (i >= 100)
            {
                return -1;
            }
        }
	}
    else
    {
        MQTTClient_destroy(&client);
        return -1;
    }
}
#endif